<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/11/29
 * Time: 14:23
 */
namespace Bstage\Controller;

class MidExamineItemController extends BstageController {

    /**
     * 添加考核项目接口
     */
    private function addMidExamineItemAction(){
        $new_data = array();
        $new_data['mid_ex_id'] = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $new_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("请输入中层考核名称");
        $new_data['sort'] = $_REQUEST['sort']?$_REQUEST['sort']:99;

        $add_result = M("MidExamineItem")
            ->add($new_data);

        if(!$add_result){
            $this->errorReturn("添加考核项目失败，请重新尝试");
        }

        $this->successReturn("添加成功");
    }

    /**
     * 修改考核项目接口
     */
    private function editMidExamineItemAction(){
        $mid_ex_id = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $item_id = $_REQUEST['item_id']?$_REQUEST['item_id']:$this->errorReturn("缺少项目ID");

        $update_data = array();
        $update_data['title'] = $_REQUEST['title']?$_REQUEST['title']:$this->errorReturn("请输入中层考核名称");
        $update_data['sort'] = $_REQUEST['sort']?$_REQUEST['sort']:$this->errorReturn("请输入排序序号");

        $update_result = M("MidExamineItem")
            ->where(['item_id'=>$item_id,'mid_ex_id'=>$mid_ex_id])
            ->save($update_data);
        if(!$update_result){
            $this->errorReturn("更新失败，请重新尝试");
        }

        $this->successReturn("修改成功");
    }

    /**
     * 获取考核项目数据接口
     */
    private function getMidExamineItemData(){
        $mid_ex_id = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $item_id = $_REQUEST['item_id']?$_REQUEST['item_id']:$this->errorReturn("缺少项目ID");

        $item_data = M("MidExamineItem")
            ->where(['item_id'=>$item_id,'mid_ex_id'=>$mid_ex_id])
            ->field("item_id,title,sort")
            ->find();
        if(!$item_data){
            $this->errorReturn("项目ID错误，请重新尝试");
        }

        $this->successReturn("获取成功",$item_data);
    }

    /**
     * 删除考核项目接口
     */
    private function deleteMidExamineItemAction(){
        $mid_ex_id = $_REQUEST['mid_ex_id']?$_REQUEST['mid_ex_id']:$this->errorReturn("缺少中层考核ID");
        $item_id = $_REQUEST['id_str']?$_REQUEST['id_str']:$this->errorReturn("缺少项目ID");

        $delete_result = M("MidExamineItem")
            ->where(['mid_ex_id'=>$mid_ex_id,'item_id'=>['in',$item_id]])
            ->delete();

        if(!$delete_result){
            $this->errorReturn("删除考核项目失败，请重新尝试");
        }

        $this->successReturn("删除考核项目成功");
    }
}